
function myValidatorIsEmail(value) {
    return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value);
}

function myValidatorIsNumeric(num) {
    return !isNaN(num);
}

function myValidatorPutError(input) {
    $(input).parent().addClass('has-error has-feedback');
    $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span>' + input.attr('data-error') + '</span>').insertAfter(input);
}

function myValidatorCleanError(input) {
    $(input).parent().removeClass('has-error has-feedback').find('span').remove();
}

function myValidatorCleanForm(form) {
    $('#' + form + ' input, #' + form + ' select').each(
            function (index) {
                var input = $(this);
                myValidatorCleanError(input);
            }
    );
}

function myValidatorValidateText(text) {
    var valField = text.val();
    if (!valField) {
        myValidatorPutError(text);
        return false;
    }
    if (valField.length > 70) {
        myValidatorPutError(text);
        return false;
    }
    return true;
}

function myValidatorValidateNumber(number) {
    var valField = number.val();
    if (!valField) {
        myValidatorPutError(number);
        return false;
    }
    if (!myValidatorIsNumeric(valField)) {
        myValidatorPutError(number);
        return false;
    }
    valField = parseInt(valField);
    if (number.attr('min')) {
        if (valField < number.attr('min')) {
            myValidatorPutError(number);
            return false;
        }
    }
    if (number.attr('max')) {
        if (valField > number.attr('max')) {
            myValidatorPutError(number);
            return false;
        }
    }
    if (valField.length < number.attr('minlength')) {
        myValidatorPutError(number);
        return false;
    }
    if (valField.length > number.attr('maxlength')) {
        myValidatorPutError(number);
        return false;
    }
    return true;
}

function myValidatorValidate(form) {
    myValidatorCleanForm(form);
    var valid = 0;
    $('#' + form + ' input, #' + form + ' select').each(
            function (index) {
                var input = $(this);
                var type = input.attr('type');
                if (type === "number") {
                    if (!myValidatorValidateNumber(input)) {
                        ++valid;
                    }
                } else if (type === "text") {
                    if (!myValidatorValidateText(input)) {
                        ++valid;
                    }
                }
            }
    );
    return valid === 0;
}

function myValidatorReadForm(form) {
    var data = [];
    $('#' + form + ' input, #' + form + ' select').each(
            function (index) {
                var input = $(this);
                var type = input.attr('type');
                if (type === "number") {
                    data.push({val: input.val(), name: input.attr('name')});
                } else if (type === "text") {
                    data.push({val: input.val(), name: input.attr('name')});
                }
            }
    );
    return data;
}

function myValidatorReadFormMap(form) {
    var data = [];
    $('#' + form + ' input, #' + form + ' select').each(
            function (index) {
                var input = $(this);
                var type = input.attr('type');
                if (type === "number") {
                    data[input.attr('name')] = parseInt(input.val());
                } else if (type === "text") {
                    data[input.attr('name')] = input.val();
                }
            }
    );
    return data;
}
